package folder;

import java.io.File;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This servlet is used for creating new Directories
 * 
 * @author cuche
 * 
 */
public class FolderCreatorServlet extends HttpServlet {
	// all static Variables that are needed
	private static final String SAVE_DIR_PATH = "/pics";
	private static final String URL_TO_FOLDER_LIST = "/folders";
	private static final String CURRENT_DIR_PARAM = "currentDir";
	private static final String FOLDER_NAME_PARAM = "folderName";
	private static final long serialVersionUID = 1L;

	public FolderCreatorServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//get current Directory and name
		String currentDir = request.getParameter(CURRENT_DIR_PARAM);
		String folderName = request.getParameter(FOLDER_NAME_PARAM);

		if (currentDir != null && folderName != null) {
			String newFolderPath = String.format("%s/%s", currentDir,
					folderName);
			File newFolder = new File(newFolderPath);
			if (!newFolder.exists()) {
				newFolder.mkdir();
			}

		}

		ServletContext context = getServletContext();
		String urlToCurrentFolder = String.format("%s?dirPath=%s",
				URL_TO_FOLDER_LIST, currentDir);
		RequestDispatcher rd = context.getRequestDispatcher(urlToCurrentFolder);
		rd.forward(request, response);
	}

}
